package com.lczyfz.istep.modules.openstack.api.neutron.impl;

import com.lczyfz.istep.modules.openstack.api.neutron.impl.parseJson.NeutronParse;
import com.lczyfz.istep.modules.openstack.api.neutron.interfaces.Networks;
import com.lczyfz.istep.modules.openstack.api.neutron.model.Network;
import com.lczyfz.istep.modules.openstack.constvary.ConstParams;
import com.lczyfz.istep.modules.openstack.constvary.RequestType;
import com.lczyfz.istep.modules.openstack.utils.HTTP;
import com.lczyfz.istep.modules.openstack.utils.Request;
import com.lczyfz.istep.modules.openstack.utils.RequestFactory;
import com.lczyfz.istep.modules.openstack.utils.Response;
import net.sf.json.JSONObject;

import java.util.List;

public class NetworksImpl implements Networks {

	public RequestFactory factory = new RequestFactory("neutron");

	@Override
	public List<Network> getNetwork() {
		List<Network> networks = null;
		Request request = factory.getRequest();
		request.setUrl("/v2.0/networks");
		request.setRequestType(RequestType.GET);
		Response results = HTTP.getConnectionResult(request);
		
		if(ConstParams.RESPONSE_CODE_NORMAL != results.getResponseCode()){
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		JSONObject body = results.getResponseBody();
		networks = NeutronParse.resolveNetworks(body);
		return networks;
	}

	@Override
	public Network getNetworkDetil(String networkID) {
		Network network = null;
		Request request = factory.getRequest();
		request.setUrl("/v2.0/networks/" + networkID);
		request.setRequestType(RequestType.GET);
		Response results = HTTP.getConnectionResult(request);
		
		if(ConstParams.RESPONSE_CODE_NORMAL != results.getResponseCode()){
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		JSONObject body = results.getResponseBody();
		//System.out.println(body);
		network = NeutronParse.resolveNetworksDetail(body);
		return network;
	}

	@Override
	public Network createNetwork(Network network) {
		Network networks = new Network();
		JSONObject params = NeutronParse.packageNetwork(network);
		
		Request request = factory.getRequest();
		request.setUrl("/v2.0/networks");
		request.setRequestType(RequestType.POST);
		request.setBody(params);
		Response results = HTTP.getConnectionResult(request);
		
		if( ConstParams.RESPONSE_CODE_CREATED!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		JSONObject res = results.getResponseBody();
		network = NeutronParse.resolveCreateNetwork(res); 
		return network;
	}

	@Override
	public Network updateNetwork(String networkID,Network network) {
		Network networks = new Network();
		JSONObject params = NeutronParse.packageNetwork(network);
		
		Request request = factory.getRequest();
		request.setUrl("/v2.0/networks/" + networkID);
		request.setRequestType(RequestType.PUT);
		request.setBody(params);
		Response results = HTTP.getConnectionResult(request);
		
		if( ConstParams.RESPONSE_CODE_NORMAL!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		JSONObject res = results.getResponseBody();
		network = NeutronParse.resolveUpdateNetwork(res);
		return network;
	}

	@Override
	public void deleteNetwork(String networkID) {
		Request request = factory.getRequest();
		request.setUrl("/v2.0/networks/" + networkID);
		request.setRequestType(RequestType.DELETE);
		Response results = HTTP.getConnectionResult(request);
		if(ConstParams.RESPONSE_CODE_SUCCESS != results.getResponseCode() )
		{
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		return;
	}
	
	//TODO:Ҫ���Ӷ� name�ļ���
	public boolean checkUniqueName(String networkName){
		return true;
	}
	
	
	public static void main(String[] args){

		NetworksImpl networkdemo = new NetworksImpl();
		System.out.println("网络列表");
//		networkdemo.deleteNetwork("b8834ab2-5947-4cc5-9a8e-4d65a1cf9440");

		
//		System.out.println("������ϸ��Ϣ");
//		Network networkdetail = networkdemo.getNetworkDetil("48042b0f-4560-40b8-8458-3b09416a5721");
//		System.out.println(networkdetail.getId() + " " + networkdetail.getName() + " " + networkdetail.getStatus() + " " + networkdetail.getSubnets());
//
//		System.out.println("创建网络");
//		Network creatNetwork = new Network();
//		creatNetwork.setName("test1");
//		Network newNetwork = networkdemo.createNetwork(creatNetwork);
//		System.out.println(newNetwork.getId() + " " + newNetwork.getName() + " " + newNetwork.getStatus());

		List<Network> networks = networkdemo.getNetwork();
		for(Network network : networks){
			System.out.println(network.getId() + " " + network.getName() + " "+ network.getStatus());

		}
//
//		System.out.println("��������");
//		newNetwork.setName("network_2");
//		Network updateNetwork = networkdemo.updateNetwork(newNetwork.getId(), newNetwork);
//		System.out.println(updateNetwork.getId() + " " + updateNetwork.getName() + " " + updateNetwork.getStatus());
//
//		System.out.println("ɾ������");
//		networkdemo.deleteNetwork(updateNetwork.getId());
//		System.out.println("��ɾ������" );
	}
}
